package com.wl.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.Map;

/**
 * @program: treeHole
 * @author: 吴磊
 * @create: 2021-07-06 22:20
 */
public class SendSmsUtils {
    /**
     * 发送短信
     *
     * @param phoneNum 手机号
     * @param code     验证码
     * @return
     */
    public static boolean sendSms(String phoneNum, Map<String, Object> code) throws ClientException {
        //连接阿里云
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G2zT3vgyD5opd1wJNcb", "FNOv4G4F01ZAPyxAj18ac2Q0xdZSUu");
        IAcsClient client = new DefaultAcsClient(profile);

        //构建请求
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.GET);
        //不要动
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");

        request.setSysAction("AddSmsSign");
        request.setSysAction("SendSms");
        //自定义参数（手机号，验证码，签名，模板）
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", "ABC商城");
        request.putQueryParameter("TemplateCode", "SMS_205449583");
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));

        CommonResponse response = client.getCommonResponse(request);
        boolean success = response.getHttpResponse().isSuccess();
        return success;
    }
}
